package com.keyinong.caizhengting.myfragement;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.keyinong.caizhengting.R;
import com.keyinong.caizhengting.tools.HttpTools;
import com.keyinong.caizhengting.adapter.MyteamAdapter;
import com.keyinong.caizhengting.entity.MyteamEntity;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class MyteamActivity extends Activity {

    /**
     * 返回
     */
    ImageButton ib_fanhui;
    /**
     * 获得数据
     */
    ArrayList<MyteamEntity> infolist;
    HttpTools httptools = null;
    String token = "";
    /**
     * 我的团队
     */

    private MyteamAdapter myteamAdapter;
    /**
     * 团队列表
     */
    private ListView lv;
    private TextView tv_more;
    private OnItemClickListener itemclicke = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int position,
                                long id) {
            Intent intent = new Intent();
            intent.setClass(MyteamActivity.this, TeamdetailActivity.class);
            intent.putExtra("teamid", infolist.get(position).getId());
            startActivity(intent);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_myteam);
        SharedPreferences sp = getSharedPreferences("spReg",
                Context.MODE_PRIVATE);
        token = sp.getString("data", "");
        lv = (ListView) findViewById(R.id.lv_myteam);
        lv.setOnItemClickListener(itemclicke);
        tv_more = (TextView) findViewById(R.id.tv_more);
        Myteam();
        ib_fanhui = (ImageButton) findViewById(R.id.ib_fanhui6);
        ib_fanhui.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();

            }
        });
    }

    private void Myteam() {
        infolist = new ArrayList<MyteamEntity>();
        if (httptools == null) {
            httptools = new HttpTools(getApplicationContext());
        }
        httptools.Myteam(token, new JsonMyTeam());

    }

    public class JsonMyTeam extends JsonHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, Header[] headers,
                              JSONObject response) {
            // TODO Auto-generated method stub
            super.onSuccess(statusCode, headers, response);
            try {
                Log.i("zq", response.toString());
                boolean code = response.getBoolean("code");
                String msg = response.getString("msg");
                if (code == true) {
                    JSONArray array = response.getJSONArray("data");
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject data = array.getJSONObject(i);
                        String id = data.getString("id");
                        String name = data.getString("name");
                        String cname = data.getString("cname");
                        String uname = data.getString("uname");
                        infolist.add(new MyteamEntity(id, name, cname, uname));
                    }
                    if (array.length() == 0) {
                        tv_more.setVisibility(View.VISIBLE);
                    } else {
                        tv_more.setVisibility(View.GONE);
                    }
                    myteamAdapter = new MyteamAdapter(getApplicationContext(), infolist);
                    lv.setAdapter(myteamAdapter);
                    lv.setDivider(null);
                } else {
                    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }


}
